<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.w3.org/1999/xhtml">
<head>
	<meta charset="UTF-8">
	<meta name="viewport"
				content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/>
	<title>在线用户</title>

	<!-- lay ui css -->
	<link th:href="@{/component/layui/css/layui.css}" rel="stylesheet">
	<!-- 自定义 css -->
	<link rel="stylesheet" th:href="@{/dimples/css/dimples.css}"/>
</head>
<body>
<div class="layui-fluid layui-anim dimples-anim" id="dimples-online" lay-title="在线用户">
	<div class="layui-row dimples-container">
		<div class="layui-col-md12">
			<div class="layui-card">
				<div class="layui-card-body dimples-table-full">
					<form class="layui-form layui-table-form" lay-filter="online-table-form">
						<div class="layui-row">
							<div class="layui-col-md11">
								<div class="layui-form-item">
									<div class="layui-inline">
										<label class="layui-form-label layui-form-label-sm">用户名</label>
										<div class="layui-input-inline">
											<label>
												<input type="text" name="username" autocomplete="off" class="layui-input">
											</label>
										</div>
									</div>
								</div>
							</div>
							<div class="layui-col-md1 table-action-area">
								<div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="query">
									<i class="layui-icon">&#xe848;</i>
								</div>
								<div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="reset">
									<i class="layui-icon">&#xe79b;</i>
								</div>
							</div>
						</div>
					</form>
					<div class="dimples-hide">
						<span class="dimples-alert-base dimples-alert-success" id="count"></span>
					</div>
					<table lay-filter="onlineTable" lay-data="{id: 'onlineTable'}"></table>
				</div>
			</div>
		</div>
	</div>
</div>
<script type="text/html" id="online-username">
	<div>
		{{d.username}}
		{{#
		if (d.current) {
		}}
		<span class="layui-badge dimples-tag-red">current</span>
		{{#
		}
		}}
	</div>
</script>
<script type="text/html" id="online-status">
	{{#
	var status = {
	1: {title: '在线', color: 'green'},
	0: {title: '离线', color: 'volcano'}
	}[d.status];
	}}
	<span class="layui-badge dimples-tag-{{status.color}}">{{ status.title }}</span>
</script>
<script type="text/html" id="online-option">
	<span shiro:lacksPermission="user:kickout">
        <span class="layui-badge-dot dimples-bg-orange"></span> 无权限
    </span>
	<a lay-event="del" shiro:hasPermission="user:kickout"><i
					class="layui-icon dimples-edit-area dimples-red">&#xe7a1;</i></a>
</script>
</body>

<!-- lay ui js -->
<script type="text/javascript" th:src="@{/component/layui/layui.js}"></script>

<script data-th-inline="none" type="text/javascript">
    layui.use(['jquery', 'form', 'table', 'pearDimples'], function () {
        var $ = layui.jquery,
            dimples = layui.pearDimples,
            form = layui.form,
            table = layui.table,
            $view = $('#dimples-online'),
            $query = $view.find('#query'),
            $reset = $view.find('#reset'),
            $searchForm = $view.find('form'),
            tableIns;

        form.render();

        initTable();

        table.on('tool(onlineTable)', function (obj) {
            var data = obj.data,
                layEvent = obj.event;
            if (layEvent === 'del') {
                dimples.modal.confirm('踢出用户', '确定将该用户踢出？', function () {
                    if (data.current) {
                        $.get('/sys/logout', function () {
                            parent.window.location.reload();
                        });
                    } else {
                        dimples.post("/session/delete/" + data.id, null, function () {
                            dimples.alert.success('踢出用户成功');
                            $query.click();
                        });
                    }
                });
            }
        });

        $query.on('click', function () {
            tableIns.reload({where: getQueryParams()});
        });

        $reset.on('click', function () {
            $searchForm[0].reset();
            tableIns.reload({where: getQueryParams()});
        });

        function initTable() {
            tableIns = dimples.table.init({
                elem: $view.find('table'),
                id: 'onlineTable',
                url: '/session/list',
                page: false,
                cols: [[
                    {title: '用户名', templet: '#online-username', minWidth: 180},
                    {field: 'startTimestamp', title: '登录时间', minWidth: 180},
                    {field: 'lastAccessTime', title: '最后访问时间', minWidth: 180},
                    {field: 'host', title: 'IP地址', minWidth: 165},
                    {field: 'location', title: '登录地点', minWidth: 180},
                    {title: '状态', templet: '#online-status'},
                    {title: '操作', toolbar: '#online-option'}
                ]],
                done: function (r) {
                    $view.find('span#count').html('当前共<strong> ' + r.count + ' </strong>人在线')
                        .parents('div.dimples-hide').show();
                }
            });
        }

        function getQueryParams() {
            return {
                username: $searchForm.find('input[name="username"]').val().trim()
            };
        }
    })
</script>
</html>














